#include <iostream>
#include <vector>
#include <string>

using namespace std;

vector<string> bag;

int main(void)
{
    int n, sum = 0;
    char c;
    string s;
    scanf("%d", &n);
    while (n--)
    {
        cin.get();
        scanf("%c", &c);
        cin >> s;
        if (c == 'I') bag.push_back(s);
        else
        {
            for (int i = 0; i < bag.size(); i++)
            {
                if (s.size() != bag[i].size()) continue;
                int j = 0; 
                for (; j < s.size(); j++)
                {
                    if (bag[i][j] == s[j]) continue;
                    break;
                }
                if (j == s.size()) sum++;
            }
            printf("%d\n", sum);
            sum = 0;
        }
    }
}